/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import sax from "./sax.js";

const xmlData =
  '<?xml version="1.0" encoding="utf-8"?>\n' +
  " <!DOCTYPE root [<!ELEMENT root (#PCDATA)>]>\n" +
  "<!-- stand alone comment -->\n" +
  "<![CDATA[This is a CDATA section.]]>\n" +
  '<dp:ListServicesReply ReturnCode="0" xmlns:dp="http://www.cisco.com/vtg/diagnosticportal">\n' +
  '  <dp:Schema Version="1.0" />\n' +
  '<script type="text/javascript">alert("Hello!");</script>\n' +
  "  <dp:ServiceList>\n" +
  '    <dp:Service Name="Cisco ICM usgd1 LoggerA" Description="Provides Call Logging services for Instance usgd1" Status="Running" StartupType="Auto" LogOnAs="****" />\n' +
  '    <dp:Service Name="Cisco ICM Diagnostic Framework" Description="Provides a web-based diagnostic service for Cisco Unified ICM, Contact Center Enterprise application." Status="Running" StartupType="Auto" LogOnAs="LocalSystem" />\n' +
  "  </dp:ServiceList>\n" +
  "</dp:ListServicesReply>";

//parser(strict:true)
function parser_Test_01() {
  let result = false;
  let parser;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser = sax.parser(true);
  }
  let endTime = Date.now();
  parser.onerror = (e) => {
    // an error happened.
  };
  parser.ontext = (t) => {
    // got some text.  t is the string of text.
  };
  parser.onopentag = (node) => {
    // opened a tag.  node has "name" and "attributes"
  };
  parser.onattribute = (attr) => {
    // an attribute.  attr has "name" and "value"
  };
  parser.onend = () => {
    // parser stream is done, and ready to have more stuff written to it.
    result = true;
  };
  parser.write(xmlData).close();
  if (result != true) {
    throw new Error("parser failed");
  }
  print(`sax_parser(strict_true): ${endTime - startTime} ms`);
}

//parser(strict:false)
function parser_Test_02() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false);
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(strict_false): ${endTime - startTime} ms`);
}

//parser(strict:false,{trim: true})
function parser_Test_03() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        trim: true
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(trim_true): ${endTime - startTime} ms`);
}

//parser(strict:false,{trim: false})
function parser_Test_04() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        trim: false
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(trim_false): ${endTime - startTime} ms`);
}

//parser(strict:false,{normalize: true})
function parser_Test_05() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        normalize: true
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(normalize_true): ${endTime - startTime} ms`);
}

//parser(strict:false,{normalize: false})
function parser_Test_06() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(true,{
        normalize: false
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(normalize_false): ${endTime - startTime} ms`);
}

//parser(strict:false,{lowercase: true})
function parser_Test_07() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        lowercase: true
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(lowercase_true): ${endTime - startTime} ms`);
}

//parser(strict:false,{lowercase: false})
function parser_Test_08() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        lowercase: false
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(lowercase_false): ${endTime - startTime} ms`);
}

//parser(strict:false,{xmlns: true})
function parser_Test_09() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        xmlns: true
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(xmlns_true): ${endTime - startTime} ms`);
}

//parser(strict:false,{xmlns: false})
function parser_Test_10() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        xmlns: false
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(xmlns_false): ${endTime - startTime} ms`);
}

//parser(strict:false,{position: true})
function parser_Test_11() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        position: true
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(position_true): ${endTime - startTime} ms`);
}

//parser(strict:false,{position: false})
function parser_Test_12() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        position: false
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(position_false): ${endTime - startTime} ms`);
}

//parser(strict:false,{strictEntities: true})
function parser_Test_13() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        strictEntities: true
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(strictEntities_true): ${endTime - startTime} ms`);
}

//parser(strict:false,{strictEntities: false})
function parser_Test_14() {
    let result = false;
    let parser;
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser = sax.parser(false,{
        strictEntities: false
      });
    }
    let endTime = Date.now();
    parser.onerror = (e) => {
      // an error happened.
    };
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
    };
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
    };
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
    };
    parser.onend = () => {
      // parser stream is done, and ready to have more stuff written to it.
      result = true;
    };
    parser.write(xmlData).close();
    if (result != true) {
      throw new Error("parser failed");
    }
    print(`sax_parser(strictEntities_false): ${endTime - startTime} ms`);
}

//parser.write
function write_Test() {
  let result;
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  parser.onerror = (e) => {
    // an error happened.
  };
  parser.ontext = (t) => {
    // got some text.  t is the string of text.
    result = t;
  };
  parser.onopentag = (node) => {
    // // opened a tag.  node has "name" and "attributes"
  };
  parser.onattribute = (attr) => {
    // an attribute.  attr has "name" and "value"
  };
  parser.onend = () => {
    // parser stream is done, and ready to have more stuff written to it.
  };
  let startTime = Date.now();
  for (let i = 0; i < 1000; i++) {
    result = parser.write(`<xml>Hello, <who name="world">world ${i}</who>!</xml>`);
  }
  let endTime = Date.now();
  result.close();
  if (result.length != 51840) {
    throw new Error("write failed");
  }
  print(`sax_write: ${endTime - startTime} ms`);
}

//parser.close
function close_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  parser.onerror = (e) => {
    // an error happened.
  };
  parser.ontext = (t) => {
    // got some text.  t is the string of text.
  };
  parser.onopentag = (node) => {
    // opened a tag.  node has "name" and "attributes"
  };
  parser.onattribute = (attr) => {
    // an attribute.  attr has "name" and "value"
  };
  parser.onend = () => {
    // parser stream is done, and ready to have more stuff written to it.
  };
  parser.write(xmlData);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    result = parser.close();
  }
  let endTime = Date.now();
  if (result.strict != false) {
    throw new Error("close failed");
  }
  print(`sax_close: ${endTime - startTime} ms`);
}

//parser.resume
function resume_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  parser.onerror = (e) => {
    // an error happened.
  };
  parser.ontext = (t) => {
    // got some text.  t is the string of text.
  };
  parser.onopentag = (node) => {
    // // opened a tag.  node has "name" and "attributes"
  };
  parser.onattribute = (attr) => {
    // an attribute.  attr has "name" and "value"
  };
  parser.onend = () => {
    // parser stream is done, and ready to have more stuff written to it.
  };
  parser.write(xmlData).close();
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 100000000; i++) {
    result = parser.resume();
  }
  let endTime = Date.now();
  result.write("\uFEFF<P></P>").close();
  if (result.strict != false) {
    throw new Error("resume failed");
  }
  print(`sax_resume: ${endTime - startTime} ms`);
}

//parser.end
function end_Test() {
  let result;
  let parser = sax.parser(true, {
    xmlns: true,
  });
  parser.onerror = (e) => {
    // an error happened.
  };
  parser.ontext = (t) => {
    // got some text.  t is the string of text.
  };
  parser.onopentag = (node) => {
    // // opened a tag.  node has "name" and "attributes"
  };
  parser.onattribute = (attr) => {
    // an attribute.  attr has "name" and "value"
  };
  parser.onend = () => {
    // parser stream is done, and ready to have more stuff written to it.
    result = true;
  };
  parser.write(xmlData);
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.end();
  }
  let endTime = Date.now();
  if (result != true) {
    throw new Error("end failed");
  }
  print(`sax_end: ${endTime - startTime} ms`);
}

//parser.flush
function flush_Test() {
  let result;
  let parser = sax.parser(false);
  parser.onopentag = (a) => {
  };
  parser.ontext = (a) => {
    result = a;
  };
  parser.onclosetag = (a) => {
  };
  parser.write("<T>flush");
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.flush();
  }
  let endTime = Date.now();
  parser.write("rest</T>");
  parser.close();
  if (result != "rest") {
    throw new Error("flush failed");
  }
  print(`sax_flush: ${endTime - startTime} ms`);
}

//parser.ontext
function ontext_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.ontext = (t) => {
      // got some text.  t is the string of text.
      result = t;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != "\n") {
    throw new Error("ontext failed");
  }
  print(`sax_ontext: ${endTime - startTime} ms`);
}

//parser.onprocessinginstruction
function onprocessinginstruction_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onprocessinginstruction = (e) => {
      result = e;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result.name != "xml" || result.body != 'version="1.0" encoding="utf-8"') {
    throw new Error("onprocessinginstruction failed");
  }
  print(`sax_onprocessinginstruction: ${endTime - startTime} ms`);
}

//parser.ondoctype
function ondoctype_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.ondoctype = (e) => {
      result = e;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != " root [<!ELEMENT root (#PCDATA)>]") {
    throw new Error("ondoctype failed");
  }
  print(`sax_ondoctype: ${endTime - startTime} ms`);
}

//parser.oncomment
function oncomment_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.oncomment = (comment) => {
      result = comment;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != " stand alone comment ") {
    throw new Error("oncomment failed");
  }
  print(`sax_oncomment: ${endTime - startTime} ms`);
}

//parser.onopentagstart
function onopentagstart_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onopentagstart = (e) => {
      result = e;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result.name != "DP:SERVICE") {
    throw new Error("onopentagstart failed");
  }
  print(`sax_onopentagstart: ${endTime - startTime} ms`);
}

//parser.onattribute
function onattribute_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onattribute = (attr) => {
      // an attribute.  attr has "name" and "value"
      result = attr;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result.name != "LOGONAS" || result.value != "LocalSystem") {
    throw new Error("onattribute failed");
  }
  print(`sax_onattribute: ${endTime - startTime} ms`);
}

//parser.onopentag
function onopentag_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onopentag = (node) => {
      // opened a tag.  node has "name" and "attributes"
      result = node;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result.name != "DP:SERVICE") {
    throw new Error("onopentag failed");
  }
  print(`sax_onopentag: ${endTime - startTime} ms`);
}

//parser.onclosetag
function onclosetag_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onclosetag = (tagName) => {
      // print(node)
      result = tagName;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != "DP:LISTSERVICESREPLY") {
    throw new Error("onclosetag failed");
  }
  print(`sax_onclosetag: ${endTime - startTime} ms`);
}

//parser.onopencdata
function onopencdata_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result = false;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onopencdata = () => {
      result = true;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != true) {
    throw new Error("onopencdata failed");
  }
  print(`sax_onopencdata: ${endTime - startTime} ms`);
}

//parser.oncdata
function oncdata_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.oncdata = (cdata) => {
      result = cdata;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != "This is a CDATA section.") {
    throw new Error("oncdata failed");
  }
  print(`sax_oncdata: ${endTime - startTime} ms`);
}

//parser.onclosecdata
function onclosecdata_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result = false;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onclosecdata = () => {
      result = true;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != true) {
    throw new Error("onclosecdata failed");
  }
  print(`sax_onclosecdata: ${endTime - startTime} ms`);
}

//parser.onerror
function onerror_Test() {
  let result;
  try {
    let strict = true; // set to false for html-mode
    let parser = sax.parser(strict);
    let startTime = Date.now();
    for (let i = 0; i < 1000000; i++) {
      parser.onerror = (e) => {
        result = e;
      };
    }
    let endTime = Date.now();
    print(`sax_onerror: ${endTime - startTime} ms`);
    parser.write(`<root attr1="first"attr2="second"/>`).close();
  } catch (error) {
    if (error != result) {
      throw new Error("onerror failed");
    }
  }
}

//parser.onend
function onend_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result = false;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onend = () => {
      result = true;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != true) {
    throw new Error("onend failed");
  }
  print(`sax_onend: ${endTime - startTime} ms`);
}

//parser.onready
function onready_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result = false;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onready = () => {
      result = true;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != true) {
    throw new Error("onready failed");
  }
  print(`sax_onready: ${endTime - startTime} ms`);
}

//parser.onscript
function onscript_Test() {
  let strict = false; // set to false for html-mode
  let parser = sax.parser(strict);
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onscript = (script) => {
      result = script;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (result != 'alert("Hello!");') {
    throw new Error("onscript failed");
  }
  print(`sax_onscript: ${endTime - startTime} ms`);
}

//parser.onopennamespace
function onopennamespace_Test() {
  let parser = sax.parser(true, {
    trim: true,
    normalize: true,
    lowercase: true,
    xmlns: true,
  });
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onopennamespace = (ns) => {
      result = ns;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (
    result.prefix != "dp" ||
    result.uri != "http://www.cisco.com/vtg/diagnosticportal"
  ) {
    throw new Error("onopennamespace failed");
  }
  print(`sax_onopennamespace: ${endTime - startTime} ms`);
}

//parser.onclosenamespace
function onclosenamespace_Test() {
  let parser = sax.parser(true, {
    trim: true,
    normalize: true,
    lowercase: true,
    xmlns: true,
  });
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 1000000; i++) {
    parser.onclosenamespace = (ns) => {
      result = ns;
    };
  }
  let endTime = Date.now();
  parser.write(xmlData).close();
  if (
    result.prefix != "dp" ||
    result.uri != "http://www.cisco.com/vtg/diagnosticportal"
  ) {
    throw new Error("onopennamespace failed");
  }
  print(`sax_onclosenamespace: ${endTime - startTime} ms`);
}

parser_Test_01();
parser_Test_02();
parser_Test_03();
parser_Test_04();
parser_Test_05();
parser_Test_06();
parser_Test_07();
parser_Test_08();
parser_Test_09();
parser_Test_10();
parser_Test_11();
parser_Test_12();
parser_Test_13();
parser_Test_14();
write_Test();
close_Test();
resume_Test();
end_Test();
flush_Test();
ontext_Test();
onprocessinginstruction_Test();
ondoctype_Test();
oncomment_Test();
onopentagstart_Test();
onattribute_Test();
onopentag_Test();
onclosetag_Test();
onopencdata_Test();
oncdata_Test();
onclosecdata_Test();
onerror_Test();
onend_Test();
onready_Test();
onscript_Test();
onopennamespace_Test();
onclosenamespace_Test();
